package com.bestcem.xm.user.dao.impl;

import com.bestcem.xm.user.dao.UserGroupLevelDao;
import com.bestcem.xm.user.dao.mapper.UserGroupLevelMapper;
import com.bestcem.xm.user.entity.mysql.UserGroupLevel;
import com.bestcem.xm.user.entity.pojo.UserGroupLevelDO;
import com.bestcem.xm.user.util.business.DataBaseUtil;
import com.bestcem.xm.user.util.convert.UserGroupLevelConvert;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户组层级信息daoImpl
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/29 10:37
 */
@Repository
public class UserGroupLevelDaoImpl implements UserGroupLevelDao {

    @Autowired
    private UserGroupLevelConvert userGroupLevelConvert;

    @Autowired
    private UserGroupLevelMapper userGroupLevelMapper;

    @Override
    public String insert(UserGroupLevelDO groupLevelDO) {
        UserGroupLevel param = userGroupLevelConvert.doToEntity(groupLevelDO);
        DataBaseUtil.setCreateAndUpdateTime(param);
        param.setGroupLevelId(DataBaseUtil.generateId());
        userGroupLevelMapper.insertSelective(param);
        return param.getGroupLevelId();
    }

    @Override
    public void insertBatch(List<UserGroupLevelDO> groupLevelDOs) {
        if (CollectionUtils.isNotEmpty(groupLevelDOs)) {
            for (UserGroupLevelDO groupLevelDO : groupLevelDOs) {
                UserGroupLevel userGroupLevel = userGroupLevelConvert.doToEntity(groupLevelDO);
                DataBaseUtil.setCreateAndUpdateTime(userGroupLevel);
                if (StringUtils.isBlank(userGroupLevel.getGroupLevelId())) {
                    userGroupLevel.setGroupLevelId(DataBaseUtil.generateId());
                }
                userGroupLevelMapper.insertSelective(userGroupLevel);
            }
        }
    }

    @Override
    public Integer deleteById(String id) {
        return userGroupLevelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteByIds(List<String> ids) {
        return userGroupLevelMapper.deleteByPrimaryKeys(ids);
    }

    @Override
    public Integer updateById(String id, UserGroupLevelDO userGroupLevelDO) {
        // 设置更新条件
        UserGroupLevel param = userGroupLevelConvert.doToEntity(userGroupLevelDO);
        DataBaseUtil.setUpdateTime(param);
        // 设置更新参数
        param.setGroupLevelId(id);
        // 更新操作
        return userGroupLevelMapper.updateByPrimaryKeySelective(param);
    }

    @Override
    public void updateByIds(List<UserGroupLevelDO> userGroupLevelDos) {
        // 设置更新条件
        List<UserGroupLevel> userGroupLevels = userGroupLevelConvert.dosToEntitys(userGroupLevelDos);
        if (CollectionUtils.isNotEmpty(userGroupLevels)) {
            for (UserGroupLevel userGroupLevel : userGroupLevels) {
                DataBaseUtil.setUpdateTime(userGroupLevel);
            }
            // 更新操作
            userGroupLevelMapper.updateBatch(userGroupLevels);
        }
    }

    @Override
    public Integer updateLevel(UserGroupLevelDO level) {
        // 设置更新条件
        UserGroupLevel param = userGroupLevelConvert.doToEntity(level);
        DataBaseUtil.setUpdateTime(param);
        // 更新操作
        return userGroupLevelMapper.updateLevelByPrimaryKey(param);
    }

    @Override
    public Integer updateByCondition(UserGroupLevelDO condition, UserGroupLevelDO userGroupLevelDO) {
        // 设置更新条件
        UserGroupLevel cond = userGroupLevelConvert.doToEntity(condition);
        // 设置更新参数
        UserGroupLevel param = userGroupLevelConvert.doToEntity(userGroupLevelDO);
        DataBaseUtil.setUpdateTime(param);
        // 更新操作
        return userGroupLevelMapper.updateBySelective(cond, param);
    }

    @Override
    public UserGroupLevelDO getById(String id) {
        return userGroupLevelConvert.entityToDo(userGroupLevelMapper.selectByPrimaryKey(id));
    }

    @Override
    public List<UserGroupLevelDO> listByIds(List<String> ids) {
        return userGroupLevelConvert.entitysToDos(userGroupLevelMapper.listByIds(ids));
    }

    @Override
    public List<UserGroupLevelDO> listByCondition(UserGroupLevelDO condition) {
        // 设置查询条件
        UserGroupLevel param = userGroupLevelConvert.doToEntity(condition);
        // 查询
        return userGroupLevelConvert.entitysToDos(userGroupLevelMapper.selectBySelective(param));
    }

    @Override
    public List<UserGroupLevelDO> getGroupLevelsByOrgId(String orgId) {
        return userGroupLevelConvert.entitysToDos(userGroupLevelMapper.getGroupLevelByOrgId(orgId));
    }

    @Override
    public Integer getLevelNumByIdJoinLevel(String groupId) {
        return userGroupLevelMapper.getLevelNumByIdJoinLevel(groupId);
    }

    @Override
    public UserGroupLevelDO getGroupByOrgIdAndName(String orgId, String groupLevelName) {
        UserGroupLevel userGrouplevel = userGroupLevelMapper.getGroupByOrgIdAndName(orgId, groupLevelName);
        return userGroupLevelConvert.entityToDo(userGrouplevel);
    }

    @Override
    public String getParentLevelId(String levelId) {
        return userGroupLevelMapper.getParentLevelId(levelId);
    }
}
